<?php

namespace App\Handlers;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use AlibabaCloud\Iot\Iot;

use App\Models\Cabinet;
use App\Models\Lock;

class AliDeviceHandler
{
    const ACCESS_KEY_ID = 'LTAIXidsHeo1vYeo';
    const ACCESS_SECRET = 'vO9c9Yqyha6PNAkFyb4jAQPfJ5rvbU'; 
    const PRODUCT_KEY = 'a1BbirCKf77';  //维控
    const LOCK_PRODUCT_KEY = 'a1biYnlmEmv';  //智能开关、蓝牙锁

    public static function initSdk(){
        AlibabaCloud::accessKeyClient(self::ACCESS_KEY_ID, self::ACCESS_SECRET)->regionId('cn-shanghai')->asDefaultClient();
    }

    public static function queryProductList(){
        self::initSdk();
        $request = Iot::v20180120()->queryProductList();
        $result = $request->withPageSize(20)->withCurrentPage(1)->request();
        return;
        // print_r($result->toArray());
    }

    /** 
     * type:1 维控,2 智能开关
     */
    public static function registerDevice($boxId,$type=1){
        self::initSdk();
        if($type==1){
            $box = Cabinet::find($boxId);
            $request = Iot::v20180120()->registerDevice();
            $result = $request->withProductKey(self::PRODUCT_KEY)->withDeviceName($box['machine_code'])->withNickname($box['name'])->request();
        }else{
            $lock = Lock::where('devicename',$boxId)->first();
            $request = Iot::v20180120()->registerDevice();
            $result = $request->withProductKey(self::LOCK_PRODUCT_KEY)->withDeviceName($lock['deviceName'])->withNickname($lock['name'])->request();
        }
        
    }

    public static function deleteDevice($machineCode,$type=1){
        self::initSdk();
        $request = Iot::v20180120()->deleteDevice();
        if($type==1){
            $result = $request->withProductKey(self::PRODUCT_KEY)->withDeviceName($machineCode)->request();
        }else{
            $result = $request->withProductKey(self::LOCK_PRODUCT_KEY)->withDeviceName($machineCode)->request();
        }
        
    }

    public static function queryDevice(){
        self::initSdk();
        $request = Iot::v20180120()->queryDevice();
        $result = $request->withProductKey(self::PRODUCT_KEY)->withPageSize(20)->withCurrentPage(1)->debug(true)->request();
        $data = $result->toArray()['Data']['DeviceInfo'];
        return $data;
    }

    public static function getDeviceStatus($machineCode){
        self::initSdk();
        $request = Iot::v20180120()->GetDeviceStatus();
        $result = $request->withProductKey(self::LOCK_PRODUCT_KEY)->withDeviceName($machineCode)->request();
        return $result->toArray();
    }

    public static function Pub($productKey,$machineCode,$Message){
        self::initSdk();
        $request = Iot::v20180120()->Pub();
        $Topic = '/'.$productKey.'/'.$machineCode.'/user/get';
        // $Message = base64_decode($Message);
        $result = $request->withProductKey($productKey)->withTopicFullName($Topic)->withMessageContent($Message)->request();
        return $result;
    }
}